using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Library;
using Framework.Library.Components.Security;
using Framework.Library.Components.Users;
using Framework.Library.Utilities;
using Framework.Module.CMS.Library.Contents;
using Framework.Module.CMS.Library.Definitions;
using Framework.Module.CMS.Library.Distributions;
using Framework.Library.Components.Commands;

namespace Framework.Module.CMS.Admin.WorkFlow
{
    public partial class VersionList : AuthenticatedControl
    {
        protected int contentid = 0;
        
        protected void Page_Load(object sender, EventArgs e)
        {            
            try
            {
                contentid = ConvertUtility.ToInt32(Request.QueryString["id"]);
            }
            catch { }
            if (contentid == 0)
            {
              //  Response.Write("<script>window.history.back()</srcipt>");
            }
            else 
            {
                dtgVersion.DataSource = ContentVersionController.GetVersionByContentId(contentid);
                dtgVersion.DataBind();
            }

        }
        protected void PostVersion(int versionid)
        {
            ContentVersionInfo vinfo = ContentVersionController.GetInfo(versionid);
            ContentInfo cinfo = ContentController.GetContent(vinfo.Content_ID);
            cinfo.Content_Alias = vinfo.Content_Alias;
            cinfo.Content_Avatar = vinfo.Content_Avatar;
            cinfo.Content_AvatarDescription = vinfo.Content_AvatarDescription;
            cinfo.Content_AvatarWidth = vinfo.Content_AvatarWidth;
            cinfo.Content_AvaterHeight = vinfo.Content_AvaterHeight;
            cinfo.Content_Body = vinfo.Content_Body;
            cinfo.SpecialAvatar = vinfo.SpecialAvatar;
            cinfo.Content_SubTitle = vinfo.Content_SubTitle;
            cinfo.Content_NoteBox = vinfo.Content_NoteBox;
            //cinfo.Content_Comment = vinfo.Content_Comment;
            cinfo.Content_Headline = vinfo.Content_Headline;
            cinfo.Content_IsComment = vinfo.Content_IsComment;
            cinfo.Content_Source = vinfo.Content_Source;
            cinfo.Content_UserID = vinfo.Content_UserID;
            cinfo.Content_ZoneID = vinfo.Content_ZoneID;
            cinfo.Content_ModifiedUserID = vinfo.Content_ModifiedUserID;
            cinfo.Content_Teaser = vinfo.Content_Teaser;
            ContentController.UpdateContent(cinfo);            
        }
        protected void dtgVersion_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int versionid = ConvertUtility.ToInt32(e.CommandArgument);
            try
            {
                switch (e.CommandName)
                {                    
                    case "Publish":
                        int usednow = ContentVersionController.Version_GetVersionUsedNow(contentid);
                        ContentVersionController.Version_SetVersionRollback(versionid, usednow);
                        PostVersion(versionid);
                        lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                        break;
                    case "deletecontent":
                        ContentVersionController.DeleteContentVersion(versionid);
                        //ContentController.SetContentStatus(contentId, (int)CMSWorkFlow.Remover);
                        break;             
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void dtgVersion_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex < 0) return;
            DataRowView curData = (DataRowView)e.Row.DataItem;            
            int versionid = ConvertUtility.ToInt32(curData["Content_Version_ID"].ToString());           
            
            HyperLink lnkHeadline = (HyperLink)e.Row.FindControl("lnkHeadline");
            lnkHeadline.Text = curData["Content_Headline"].ToString();
            lnkHeadline.NavigateUrl = AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.CMS_ViewContent)) + "&versionid=" + versionid.ToString();
            if(ConvertUtility.ToBoolean(curData["DeployedVersion"].ToString())==true)
                lnkHeadline.Font.Underline =true;
            Button cmdDel = (Button)e.Row.FindControl("cmdDel");
            cmdDel.Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);

            if ((CurrentUser.UserID == 1) || (CurrentUser.UserID == ConvertUtility.ToInt32(curData["CheckOutByUser"])) || (ConvertUtility.ToInt32(curData["CheckOutByUser"])) == 0)
            {
                Button cmdPost = (Button)e.Row.FindControl("cmdPost");
                Button cmdEdit = (Button)e.Row.FindControl("cmdEdit");
                cmdPost.Enabled = true;               
                cmdDel.Enabled = true;

            }
            else
            {
                Button cmdPost = (Button)e.Row.FindControl("cmdPost");
                Button cmdEdit = (Button)e.Row.FindControl("cmdEdit");
                cmdPost.Enabled = false;
                cmdDel.Enabled = false;
            }
        }
    }
}